﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Labb_3.Model;

namespace Labb3.View
{
    class CustomSystem
    {
        private Vector2 _startingPosition;

        private float _shockwaveDiameter;

        private int _numberOfSmokes = 100;
        private int _numberOfSplitter = 50;

        SplitterParticle[] _splitterArray;
        Shockwave _shockwave;
        SmokeParticle[] _smokeArray;
        Camera _camera;

        public CustomSystem(Camera camera, Vector2 startingPosition, float mouseOffset)
        {
            _camera = camera;

            //_startingPosition = _camera.GetLogicalCoords(startingPosition);
            _startingPosition = new Vector2(_camera.GetLogicalCoords(startingPosition).X + mouseOffset / 2, _camera.GetLogicalCoords(startingPosition).Y + mouseOffset / 2);
            _splitterArray = new SplitterParticle[_numberOfSplitter];
            _smokeArray = new SmokeParticle[_numberOfSmokes];
            _shockwaveDiameter = 1f;
        }

        internal void LoadContent(ContentManager contManager)
        {
            Texture2D shockwaveTexture = contManager.Load<Texture2D>("shockwave");
            Texture2D sparkTexture = contManager.Load<Texture2D>("spark");
            Texture2D smokeTexture = contManager.Load<Texture2D>("particlesmoke");

            SoundEffect soundEffect = contManager.Load<SoundEffect>("fire");
            soundEffect.Play();

            for (int i = 0; i < _numberOfSplitter; i++)
            {
                SplitterParticle splitter = new SplitterParticle(_startingPosition, i, sparkTexture, _camera);
                _splitterArray[i] = splitter;
            }
            for (int i = 0; i < _numberOfSmokes; i++)
            {
                SmokeParticle smoke = new SmokeParticle(_startingPosition, i, smokeTexture, _camera);
                _smokeArray[i] = smoke;
            }
            _shockwave = new Shockwave(_startingPosition, shockwaveTexture, _shockwaveDiameter, _camera);
        }

        internal void Update(GameTime gameTime)
        {
            foreach (SplitterParticle splitter in _splitterArray)
            {
                splitter.Update(gameTime);
            }
            foreach (SmokeParticle smoke in _smokeArray)
            {
                smoke.Update(gameTime);
            }
            _shockwave.Update(gameTime);
        }

        internal void Draw(SpriteBatch spritebatch)
        {
            _shockwave.Draw(spritebatch);
            foreach (SplitterParticle splitter in _splitterArray)
            {
                splitter.Draw(spritebatch);
            }
            foreach (SmokeParticle smoke in _smokeArray)
            {
                smoke.Draw(spritebatch);
            }
        }        
    }
}
